package com.xuwei.base.link;

/**
 * 双链表
 * @author David
 * @since 2015年6月14日 下午1:32:45
 * @version 
 * @since JDK 1.6
 */
public class DoubleLink {
	DLNode head;

	public DoubleLink() {
		head = new DLNode();
		head.setPrior(null);
		head.setRear(null);
	}

	public void createLink(int[] arr) {
		DLNode newNode;
		DLNode tair = new DLNode();
		tair = head;

		for (int a : arr) {
			newNode=new DLNode();
			tair.setRear(newNode);
			newNode.setVal(a);
			newNode.setPrior(tair);
			newNode.setRear(null);
			tair=newNode;
		}
	}
	
	public void printLink(){
		DLNode p=head.getRear();
		while(p!=null){
			System.out.print(p.getVal()+",");
			p=p.getRear();
		}
		System.out.print("null");
	}
	
	public DLNode searchNodeByVal(int val){
		DLNode p=head.getRear();
		while(p!=null){
			if(p.getVal()==val){
				break;
			}
			p=p.getRear();
		}
		return p;
	}
}